package net.sourceforge.jweb.jstl.fn;

import java.util.Locale;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	static final String AB = "23456789ABCDEFGHJKMNPQRSTUVWXYZ";
	static Random rnd = new Random();

	public static String random(int len) {
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++)
			sb.append(AB.charAt(rnd.nextInt(AB.length())));
		return sb.toString();
	}

	/**
	 * remove \A\D
	 * 
	 * @param source
	 * @return
	 */
	public static String normalize(String source) {
		if (source == null)
			return source;
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < source.length(); i++) {
			if (source.charAt(i) == 10)
				continue;
			else if (source.charAt(i) == 13)
				continue;
			else if (source.charAt(i) == '\t')
				continue;
			else if (source.charAt(i) == ' ')
				continue;
			else
				builder.append(source.charAt(i));
		}
		return builder.toString();
	}

	public static char charAt(String s, int index) {
		if (s == null)
			return ' ';
		return s.charAt(index);
	}

	public static int codePointAt(String source, int index) {
		if (source == null)
			return -1;
		return source.codePointAt(index);
	}

	public static int codePointBefore(String s, int index) {
		if (s == null)
			return -1;
		return s.codePointBefore(index);
	}

	public static int codePointCount(String s, int beginIndex, int endIndex) {
		if (s == null)
			return -1;
		return s.codePointCount(beginIndex, endIndex);
	}

	public static int compareTo(String s, String anotherString) {
		if (s == null && anotherString == null)
			return 0;
		if (s == null || anotherString == null)
			return -1;
		return s.compareTo(anotherString);
	}

	public static int compareToIgnoreCase(String s, String str) {
		if (s == null && str == null)
			return 0;
		if (s == null || str == null)
			return -1;
		return s.compareToIgnoreCase(str);
	}

	public static String concat(String s, String str) {
		if (s == null)
			return str;
		if (str == null)
			return s;
		return s.concat(str);
	}

	public static boolean contains(String s, CharSequence cs) {
		if (s == null || cs == null)
			return false;
		return s.contains(cs);
	}

	public static boolean contentEquals(String s, CharSequence cs) {
		if (s == null || cs == null)
			return false;
		return s.contentEquals(cs);
	}

	public static String copyValueOf(String s) {
		if (s == null)
			return "";
		return String.copyValueOf(s.toCharArray());
	}

	public static String copyValueOf(String s, int offset, int count) {
		if (s == null)
			return "";
		return String.copyValueOf(s.toCharArray(), offset, count);
	}

	public static boolean endsWith(String s, String suffix) {
		if (s == null || suffix == null)
			return false;
		return s.endsWith(suffix);
	}

	public static boolean equals(String s, Object obj) {
		if (s == null || obj == null)
			return false;
		return s.equals(obj);
	}

	public static boolean equalsIgnoreCase(String s, String anotherString) {
		if (s == null || anotherString == null)
			return false;
		return s.equalsIgnoreCase(anotherString);
	}

	public static String format(Locale l, String format, Object[] args) {
		if (l == null || format == null || args == null)
			return format;
		return String.format(l, format, args);
	}

	public static String format(String format, Object[] args) {
		if (format == null || args == null)
			return format;
		return String.format(format, args);
	}

	public static int hashCode(String s) {
		if (s == null)
			return 0;
		return s.hashCode();
	}

	public static int indexOf(String s, int ch) {
		if (s == null)
			return -1;
		return s.indexOf(ch);
	}

	public static int indexOf(String s, int ch, int fromIndex) {
		if (s == null)
			return -1;
		return s.indexOf(ch, fromIndex);
	}

	public static int indexOf(String s, String ch) {
		if (s == null)
			return -1;
		return s.indexOf(ch);
	}

	public static int indexOf(String s, String ch, int fromIndex) {
		if (s == null || ch == null)
			return -1;
		return s.indexOf(ch, fromIndex);
	}

	public static String intern(String s) {
		if (s == null)
			return "";
		return s.intern();
	}

	public static boolean isEmpty(String s) {
		if (s == null)
			return true;
		return s.isEmpty();
	}

	public static int lastIndexOf(String s, String ch) {
		if (s == null)
			return -1;
		return s.indexOf(ch);
	}

	public static int lastIndexOf(String s, String ch, int fromIndex) {
		if (s == null || ch == null)
			return -1;
		return s.indexOf(ch, fromIndex);
	}

	public static int lastIndexOf(String s, int ch) {
		if (s == null)
			return -1;
		return s.indexOf(ch);
	}

	public static int lastIndexOf(String s, int ch, int fromIndex) {
		if (s == null)
			return -1;
		return s.indexOf(ch, fromIndex);
	}

	public static int length(String s) {
		if (s == null)
			return 0;
		return s.length();
	}

	public static boolean matches(String s, String regex) {
		if (s == null || regex == null)
			return false;
		return s.matches(regex);
	}

	public static int offsetByCodePoints(String s, int index, int codePointOffset) {
		if (s == null)
			return -1;
		return s.offsetByCodePoints(index, codePointOffset);
	}

	public static boolean regionMatches(String s, boolean ignoreCase, int toffset, String other, int ooffset, int len) {
		if (s == null)
			return false;
		return s.regionMatches(toffset, other, ooffset, len);
	}

	public static boolean regionMatches(String s, int toffset, String other, int ooffset, int len) {
		if (s == null || other == null)
			return false;
		return s.regionMatches(toffset, other, ooffset, len);
	}

	public static String replace(String s, CharSequence target, CharSequence replacement) {
		if (s == null || target == null || replacement == null)
			return s;
		return s.replace(target, replacement);
	}

	public static String replaceAll(String s, String regex, String replacement) {
		if (s == null || regex == null || replacement == null)
			return s;
		return s.replaceAll(regex, replacement);
	}

	public static String replaceFirst(String s, String regex, String replacement) {
		if (s == null || regex == null || replacement == null)
			return s;
		return s.replaceFirst(regex, replacement);
	}

	public static String[] split(String s, String regex) {
		if (s == null || regex == null)
			return new String[] {};
		return s.split(regex);
	}

	public static String[] split(String s, String regex, int limit) {
		if (s == null || regex == null)
			return new String[] {};
		return s.split(regex, limit);
	}

	public static boolean startsWith(String s, String prefix) {
		if (s == null || prefix == null)
			return false;
		return s.startsWith(prefix);
	}

	public static boolean startsWith(String s, String prefix, int toffset) {
		if (s == null || prefix == null)
			return false;
		return s.startsWith(prefix, toffset);
	}

	public static String substring(String s, int beginIndex) {
		if (s == null)
			return s;
		return s.substring(beginIndex);
	}

	public static String substring(String s, int beginIndex, int endIndex) {
		if (s == null)
			return s;
		return s.substring(beginIndex, endIndex);
	}

	public static char[] toCharArray(String s) {
		if (s == null)
			return new char[] {};
		return s.toCharArray();
	}

	public static String toLowerCase(String s) {
		if (s == null)
			return s;
		return s.toLowerCase();
	}

	public static String toLowerCase(String s, Locale l) {
		if (s == null || l == null)
			return s;
		return s.toLowerCase(l);
	}

	public static String toUpperCase(String s) {
		if (s == null)
			return s;
		return s.toUpperCase();
	}

	public static String toUpperCase(String s, Locale l) {
		if (s == null || l == null)
			return s;
		return s.toUpperCase(l);
	}

	public static String trim(String s) {
		if (s == null)
			return s;
		return s.trim();
	}

	public static String valueOf(boolean v) {
		return String.valueOf(v);
	}

	public static String valueOf(char v) {
		return String.valueOf(v);
	}

	public static String valueOf(char[] v) {
		return String.valueOf(v);
	}

	public static String valueOf(char[] v, int offset, int count) {
		return String.valueOf(v, offset, count);
	}

	public static String valueOf(double v) {
		return String.valueOf(v);
	}

	public static String valueOf(float v) {
		return String.valueOf(v);
	}

	public static String valueOf(int v) {
		return String.valueOf(v);
	}

	public static String valueOf(long v) {
		return String.valueOf(v);
	}

	public static String valueOf(Object v) {
		return String.valueOf(v);
	}

	public static boolean getRichBoolean(String s) {
		if (s == null)
			return false;
		return "yes".equalsIgnoreCase(s) || "1".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s) || "true".equals(s);
	}

	public static String retains(String s, int length, String omit) {
		if (s == null)
			return "";
		if (s.length() < length)
			return s;
		return s.substring(0, length) + omit;
	}

	/**
	 * fill parameters to a expression likes ${0}.....${1}......
	 * 
	 * @param exp
	 * @param vals
	 * @return
	 */
	public static String fillParameter(String exp, String... vals) {
		if (exp == null || vals == null)
			return exp;
		for (int i = 0; i < vals.length; i++) {
			exp = exp.replaceFirst("\\$\\{" + i + "\\}", vals[i]);
		}
		exp = exp.replaceAll("\\$\\{\\d\\}", "");
		return exp;
	}

	public static String concat(String... str) {
		if (str == null)
			return "";
		StringBuilder b = new StringBuilder();
		if (str != null) {
			for (String s : str) {
				b.append(s);
			}
		}
		return b.toString();
	}

	public static String uuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static String regexMatchFirst(String source, String pattern, int option) {
		if (source == null)
			return "";
		Pattern regex = Pattern.compile(pattern, option);
		Matcher m = regex.matcher(source);
		while (m.find()) {
			return m.group();
		}
		return "";
	}

	public static String regexMatchFirst(String source, String pattern) {
		if (source == null)
			return "";
		Pattern regex = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
		Matcher m = regex.matcher(source);
		while (m.find()) {
			return m.group();
		}
		return "";
	}
}
